import { takeEvery } from 'redux-saga';
import { put, call } from 'redux-saga/effects'
import { LOAD, LOAD_SUCCESS, LOAD_FAIL } from './constants';
import {ax_get} from '../../utils/ax_agent'
import util from 'util'

export default function* postSaga() {
  yield [
    getCommentListData()
  ]
}

function* fetchCommentList(action) {
   try {

      console.log(util.inspect(action, false, null))
      const page_number = action.payload.page_number;
      const post_id = action.payload.post_id;

      const result = yield call( ax_get,  '/api/comment/getCommentsByGamePostPaged', 
        {
          params: 
          {
            page_number: page_number,
            post_id: post_id
          }
        } 
        )

      yield put({type: LOAD_SUCCESS, result: result.data})
   
   } catch (e) {

      yield put({type: LOAD_FAIL, message: e.message})

   }
}

function* getCommentListData() {
  yield* takeEvery( LOAD, fetchCommentList)
}